/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package Wires.impl;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.m2m.atl.engine.AtlEMFModelHandler;

import Wires.CompositeTransformationType;
import Wires.InputFormalParameter;
import Wires.ModelType;
import Wires.WiresElement;
import Wires.WiresPackage;
import Wires.WiresSpecification;
import Wires.exep.ArgumentNotFoundExeption;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Composite Transformation Type</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link Wires.impl.CompositeTransformationTypeImpl#getEls <em>Els</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class CompositeTransformationTypeImpl extends TransformationTypeImpl implements CompositeTransformationType {
	
	/***************************************************/
	/***************** Modifications *******************/
	
	@SuppressWarnings("unchecked")
	protected void instanciateArguments(Map<String,String> args, boolean plugin){		
		AtlEMFModelHandler modelHandler = (AtlEMFModelHandler) AtlEMFModelHandler.getDefault(AtlEMFModelHandler.AMH_EMF);
		
		List<FormalParameterImpl> formalParameters = new LinkedList<FormalParameterImpl>();
		formalParameters.addAll((Collection<? extends FormalParameterImpl>) this.getInParams());
		formalParameters.addAll((Collection<? extends FormalParameterImpl>) this.getOutParams());
		
		for (FormalParameterImpl p : formalParameters){
			boolean uri = false;
			boolean mmFromArgs = false;
			String mmpath = null;
			if (p.getTypeEl() == null){ // Metamodel from arguments				
				mmpath = args.get(p.getTypeName());
				mmFromArgs = true;
			}else if (p.getTypeEl().getPath() != null && !p.getTypeEl().getPath().equals("")){ // Metamodel specified on the diagram
				mmpath = p.getTypeEl().getPath();										
			}else if (!(p.getTypeEl() instanceof BasicDataTypeImpl)){ // Metamodel by Uri
				mmpath = ((ModelType)p.getTypeEl()).getUri();
				uri = true;
			}
			
			if ((mmpath == null || mmpath.equals("")) && !(p.getTypeEl() instanceof BasicDataTypeImpl))
				throw new ArgumentNotFoundExeption(p);			
			
			if (!(p.getTypeEl() instanceof BasicDataTypeImpl)){		
				if (!uri){
					if (plugin && !mmFromArgs){		// The metamodel file is located into the plug-in		
						mmpath = mmpath.substring(mmpath.indexOf("\\") + 1);
						mmpath = mmpath.substring(mmpath.indexOf("\\") + 1);
						mmpath = WiresLauncher.RESOURCES_LOCATION + mmpath;
					}else{ // The metamodel File is located in the plug-in user's workspace
						mmpath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString() + mmpath;
					}
					
					p.metamodel = modelHandler.loadModel(p.getTypeName(), modelHandler.getMof(), URI.createFileURI(mmpath));
				}else{			
					p.metamodel = modelHandler.loadModel(p.getTypeName(), modelHandler.getMof(), "uri:" + mmpath);
				}
				
			}
		
			
			String modelpath = null;
			if (p.getPath() == null || p.getPath().equals("")){ // Model from arguments
				modelpath = args.get(p.getName());				
			}else{ // Model specified on the diagram
				modelpath = p.getPath();				
			}			
			
			if (modelpath == null || modelpath.equals(""))
				throw new ArgumentNotFoundExeption(p);
			
			modelpath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString() + modelpath;
			
			p.setPath(modelpath);
			
			if (p instanceof InputFormalParameter){
				p.model = modelHandler.loadModel(p.getName(), p.metamodel, URI.createFileURI(modelpath));
				p.setModelPath(modelpath);
			}
		}	
	}
	
	@SuppressWarnings("unchecked")
	protected void instanciateArgumentsStandAlone(Map<String,String> args){		
		AtlEMFModelHandler modelHandler = (AtlEMFModelHandler) AtlEMFModelHandler.getDefault(AtlEMFModelHandler.AMH_EMF);
		
		List<FormalParameterImpl> formalParameters = new LinkedList<FormalParameterImpl>();
		formalParameters.addAll((Collection<? extends FormalParameterImpl>) this.getInParams());
		formalParameters.addAll((Collection<? extends FormalParameterImpl>) this.getOutParams());
		
		for (FormalParameterImpl p : formalParameters){
			boolean uri = false;
			String mmpath = null;
			if (p.getTypeEl() == null){ // Metamodel from arguments				
				mmpath = args.get(p.getTypeName());
			}else if (p.getTypeEl().getPath() != null && !p.getTypeEl().getPath().equals("")){ // Metamodel specified on the diagram
				mmpath = p.getTypeEl().getPath();										
			}else if (!(p.getTypeEl() instanceof BasicDataTypeImpl)){ // Metamodel by Uri
				mmpath = ((ModelType)p.getTypeEl()).getUri();
				uri = true;
			}
			
			if ((mmpath == null || mmpath.equals("")) && !(p.getTypeEl() instanceof BasicDataTypeImpl))
				throw new ArgumentNotFoundExeption(p);			
			
			if (!(p.getTypeEl() instanceof BasicDataTypeImpl)){		
				if (!uri){
					p.metamodel = modelHandler.loadModel(p.getTypeName(), modelHandler.getMof(), URI.createFileURI(mmpath));
				}else{			
					p.metamodel = modelHandler.loadModel(p.getTypeName(), modelHandler.getMof(), "uri:" + mmpath);
				}
				
			}
		
			
			String modelpath = null;
			if (p.getPath() == null || p.getPath().equals("")){ // Model from arguments
				modelpath = args.get(p.getName());				
			}else{ // Model specified on the diagram
				modelpath = p.getPath();				
			}			
			
			if (modelpath == null || modelpath.equals(""))
				throw new ArgumentNotFoundExeption(p);

			p.setPath(modelpath);
			
			if (p instanceof InputFormalParameter){
				p.model = modelHandler.loadModel(p.getName(), p.metamodel, URI.createFileURI(modelpath));
				p.setModelPath(modelpath);
			}
		}	
	}
	
	/**************** Modifications End ****************/
	/***************************************************/
	
	
	/**
	 * The cached value of the '{@link #getEls() <em>Els</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getEls()
	 * @generated
	 * @ordered
	 */
	protected EList<WiresElement> els;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected CompositeTransformationTypeImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return WiresPackage.Literals.COMPOSITE_TRANSFORMATION_TYPE;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<WiresElement> getEls() {
		if (els == null) {
			els = new EObjectContainmentEList<WiresElement>(WiresElement.class, this, WiresPackage.COMPOSITE_TRANSFORMATION_TYPE__ELS);
		}
		return els;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case WiresPackage.COMPOSITE_TRANSFORMATION_TYPE__ELS:
				return ((InternalEList<?>)getEls()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case WiresPackage.COMPOSITE_TRANSFORMATION_TYPE__ELS:
				return getEls();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case WiresPackage.COMPOSITE_TRANSFORMATION_TYPE__ELS:
				getEls().clear();
				getEls().addAll((Collection<? extends WiresElement>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case WiresPackage.COMPOSITE_TRANSFORMATION_TYPE__ELS:
				getEls().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case WiresPackage.COMPOSITE_TRANSFORMATION_TYPE__ELS:
				return els != null && !els.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eBaseStructuralFeatureID(int derivedFeatureID, Class<?> baseClass) {
		if (baseClass == WiresSpecification.class) {
			switch (derivedFeatureID) {
				case WiresPackage.COMPOSITE_TRANSFORMATION_TYPE__ELS: return WiresPackage.WIRES_SPECIFICATION__ELS;
				default: return -1;
			}
		}
		return super.eBaseStructuralFeatureID(derivedFeatureID, baseClass);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eDerivedStructuralFeatureID(int baseFeatureID, Class<?> baseClass) {
		if (baseClass == WiresSpecification.class) {
			switch (baseFeatureID) {
				case WiresPackage.WIRES_SPECIFICATION__ELS: return WiresPackage.COMPOSITE_TRANSFORMATION_TYPE__ELS;
				default: return -1;
			}
		}
		return super.eDerivedStructuralFeatureID(baseFeatureID, baseClass);
	}

} //CompositeTransformationTypeImpl
